package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.InventoryInoutJob;
import com.wing.order.model.request.InventoryJobForm;
import com.wing.order.model.response.InventoryInoutJobVO;
import com.wing.order.model.response.InventoryJobLabelVO;
import com.wing.order.model.response.InventoryJobProductVO;
import com.wing.order.model.response.InventoryOutPlanVO;
import com.wing.order.service.InventoryInoutJobService;
import com.wing.order.service.InventoryJobLabelService;
import com.wing.order.service.InventoryJobProductService;
import com.wing.order.service.InventoryOutPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-出库作业记录列表", tags = "#/order/inventoryOutWorkRecord")
@RestController("inventoryOutWorkRecordController")
@RequestMapping("order/admin/inventoryOutWorkRecord")
@RequiredArgsConstructor
public class InventoryOutWorkRecordController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryInoutJobService inventoryInoutJobService;
    private final InventoryJobLabelService inventoryJobLabelService;
    private final InventoryJobProductService inventoryJobProductService;
    @ApiOperation(value = "查询所有出库单计划", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutJobVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , InventoryInoutJobVO inventoryInoutJobVO
    ){

        //搜索
        IPage<InventoryInoutJobVO> list = inventoryOutPlanService.getInventoryOutWorkRecordPageList(inventoryInoutJobVO,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/getInventoryOutPlanById")
    public JsonResult getInventoryOutPlanById(@RequestParam(value = "id") Long id){
        InventoryOutPlanVO info = inventoryOutPlanService.getInventoryOutPlanById(id);
        return JsonResult.success(info);
    }

    @ApiOperation(value = "添加出库作业单记录", notes = "添加")
    @PostMapping("/addOutInventoryInoutJob")
    public JsonResult add(@Validated(InventoryJobForm.Add.class) InventoryJobForm inventoryJobForm) {
       try {
           inventoryInoutJobService.addOutInventoryInoutJob(inventoryJobForm);
           return JsonResult.success("操作成功！");
        } catch (Exception e) {
           return JsonResult.fail("操作失败:" + e.getMessage());
        }

    }

    @ApiOperation(value = "查询出库产品信息详情", notes = "添加")
    @GetMapping("/getOutWorkProductDetail")
    public JsonResult getOutWorkProductDetail(@RequestParam(value = "jobId") Long jobId){
        List<InventoryJobProductVO> productVOList = inventoryJobProductService.getOutWorkProductDetail(jobId);
        for (InventoryJobProductVO item : productVOList){
            item.setTotalMoney(new BigDecimal(item.getQuantity()).multiply(item.getTotal()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return JsonResult.success(productVOList);
    }

    @ApiOperation(value = "查询出库作业记录产品明细的包装码数据", notes = "添加")
    @GetMapping("/getOutWorkPackList")
    public JsonResult getOutWorkPackList(@RequestParam(value = "jobId") Long jobId,@RequestParam(value = "jobProductId") Long jobProductId){
        List<InventoryJobLabelVO> outWorkPackList = inventoryJobLabelService.getOutWorkPackList(jobId,jobProductId);
        return JsonResult.success(outWorkPackList);
    }
    @ApiOperation(value = "审核出库作业单", notes = "修改")
    @PutMapping("/updateStatus")
    public JsonResult updateStatus(@RequestParam(value = "id") Long id) {
        InventoryInoutJob inoutJob = inventoryInoutJobService.getById(id);
        if(inoutJob == null){
            return JsonResult.fail("修改失败！");
        }
        inoutJob.setStatus(InventoryInoutJob.Status.confirm);
        if (inventoryInoutJobService.updateById(inoutJob)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }
}
